locals {
  prepaid_settings           = var.ecs_internet_billing_type == "PostPaid" ? {} : var.prepaid_settings
}

resource "alicloud_instance" "main" {
  count                      = var.number_of_instances  
  security_groups            = var.ecs_sg_id
  instance_type              = var.ecs_instance_type
  system_disk_category       = var.ecs_disk_type
  system_disk_size           = var.ecs_disk_size
  image_id                   = var.ecs_image
  security_enhancement_strategy = var.ecs_image_security
  instance_name              = var.ecs_name
  vswitch_id                 = var.ecs_vswitch_id
  host_name                  = var.ecs_hostname
  internet_max_bandwidth_out = var.ecs_max_bandwidth_out
  internet_charge_type       = var.ecs_internet_billing_type
  instance_charge_type       = var.ecs_instance_billing_type
  key_name                   = var.ecs_key_name  
  period                     = lookup(local.prepaid_settings, "period", null)
  period_unit                = lookup(local.prepaid_settings, "period_unit", null)
  renewal_status             = lookup(local.prepaid_settings, "renewal_status", null)
  auto_renew_period          = lookup(local.prepaid_settings, "auto_renew_period", null)
  include_data_disks         = lookup(local.prepaid_settings, "include_data_disks", null)
  dry_run                    = var.dry_run
  role_name                  = var.role_name
  spot_strategy              = var.spot_strategy
  spot_price_limit           = var.spot_price_limit
  deletion_protection        = var.deletion_protection
  dynamic "data_disks" {
    for_each = var.ecs_data_disks
    content {
      name                   = lookup(data_disks.value, "name", null)
      size                   = lookup(data_disks.value, "size", null)
      category               = lookup(data_disks.value, "category", null)
      encrypted              = lookup(data_disks.value, "encrypted", null)
      snapshot_id            = lookup(data_disks.value, "snapshot_id", null)
      delete_with_instance   = lookup(data_disks.value, "delete_with_instance", null)
      description            = lookup(data_disks.value, "description", null)
    }
  }
  tags                      = var.tags
  volume_tags               = var.tags
  user_data                  = <<HEREDOC
#!/bin/bash 
set -x
export OSSBUCKET_TERRAFORM="${var.userdata_TERRAFORM_OSSBUCKET}"
export HOME="/opt"
export endpoint="oss-cn-shenzhen-internal.aliyuncs.com"
export accessKeyID=$(curl -s http://100.100.100.200/latest/meta-data/Ram/security-credentials/${var.role_name}|grep AccessKeyId |awk -F\" '{print $4}')
export accessKeySecret=$(curl -s http://100.100.100.200/latest/meta-data/Ram/security-credentials/${var.role_name}|grep AccessKeySecret |awk -F\" '{print $4}')
export stsToken=$(curl -s http://100.100.100.200/latest/meta-data/Ram/security-credentials/${var.role_name}|grep SecurityToken |awk -F\" '{print $4}')

name=`uname  -a`
centos="Centos"
ubuntu="Ubuntu"

if [[ $name =~ $centos ]];then
  yum -y update
  yum -y install epel-release
  yum -y install python-pip
elif [[ $name =~ $ubuntu ]];then
  apt update
  apt -y install python-pip software-properties-common  
else
    echo $name
fi

pip install --upgrade pip
pip install ansible==2.7.0
curl -o /usr/local/bin/ossutil http://gosspublic.alicdn.com/ossutil/1.6.9/ossutil64  
chmod +x /usr/local/bin/ossutil                          

ossutil config -e $${endpoint} -i $${accessKeyID} -k $${accessKeySecret} -t $${stsToken} 
ossutil cp -r oss://$${OSSBUCKET_TERRAFORM}/playbooks/public_keys $${HOME}/ops-terraform/playbooks/common --update
ossutil cp -r oss://$${OSSBUCKET_TERRAFORM}/playbooks/roles $${HOME}/ops-terraform/playbooks/common --update

mkdir $${HOME}/ops-terraform/playbooks/projects/
ln -s $${HOME}/ops-terraform/playbooks/common/roles $${HOME}/ops-terraform/playbooks/projects/roles

cp_chmod_exec () { 
    PLAYBOOK=$1
    echo $PLAYBOOK >> exec.txt
    ossutil cp oss://$${OSSBUCKET_TERRAFORM}/$PLAYBOOK $${HOME}/ops-terraform/$PLAYBOOK --update
    chmod 400 $${HOME}/ops-terraform/$PLAYBOOK
    ansible-playbook -v $${HOME}/ops-terraform/$PLAYBOOK
}
list_playbooks="${join(" ", var.userdata_VAR_PLAYBOOKS)}"
for playbook in $${list_playbooks}
do
	cp_chmod_exec $playbook
done
HEREDOC
}